home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / bison.arc / LR0.C < prev    next >
C/C++ Source or Header  |  1988-07-11  |  14KB  |  655 lines

  1. /* Generate the nondeterministic finite state machine for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* See comments in state.h for the data structures that represent it.
  22.    The entry point is generate_states.  */
  23.  
  24. #include <stdio.h>
  25. #include "machine.h"
  26. #include "new.h"
  27. #include "gram.h"
  28. #include "state.h"
  29.  
  30.  
  31. extern char *nullable;
  32. extern short *itemset;
  33. extern short *itemsetend;
  34.  
  35.  
  36. int nstates;
  37. int final_state;
  38. core *first_state;
  39. shifts *first_shift;
  40. reductions *first_reduction;
  41.  
  42. int get_state();
  43. core *new_state();
  44.  
  45. static core *this_state;
  46. static core *last_state;
  47. static shifts *last_shift;
  48. static reductions *last_reduction;
  49.  
  50. static int nshifts;
  51. static short *shift_symbol;
  52.  
  53. static short *redset;
  54. static short *shiftset;
  55.  
  56. static short **kernel_base;
  57. static short **kernel_end;
  58. static short *kernel_items;
  59.  
  60. /* hash table for states, to recognize equivalent ones.  */
  61.  
  62. #define    STATE_TABLE_SIZE    1009
  63. static core **state_table;
  64.  
  65.  
  66.  
  67. allocate_itemsets()
  68. {
  69.   register short *itemp;
  70.   register int symbol;
  71.   register int i;
  72.   register count;
  73.   register int max;
  74.   register short *symbol_count;
  75.  
  76.   count = 0;
  77.   symbol_count = NEW2(nsyms, short);
  78.  
  79.   itemp = ritem;
  80.   symbol = *itemp++;
  81.   while (symbol)
  82.     {
  83.       if (symbol > 0)
  84.     {
  85.       count++;
  86.       symbol_count[symbol]++;
  87.     }
  88.       symbol = *itemp++;
  89.     }
  90.  
  91.   /* see comments before new-itemset.  All the vectors of items
  92.      live inside kernel_items.  The number of active items after
  93.      some symbol cannot be more than the number of times that symbol
  94.      appears as an item, which is symbol_count[symbol].
  95.      We allocate that much space for each symbol.  */
  96.  
  97.   kernel_base = NEW2(nsyms, short *);
  98.   kernel_items = NEW2(count, short);
  99.  
  100.   count = 0;
  101.   max = 0;
  102.   for (i = 0; i < nsyms; i++)
  103.     {
  104.       kernel_base[i] = kernel_items + count;
  105.       count += symbol_count[i];
  106.       if (max < symbol_count[i])
  107.     max = symbol_count[i];
  108.     }
  109.  
  110.   shift_symbol = symbol_count;
  111.   kernel_end = NEW2(nsyms, short *);
  112. }
  113.  
  114.  
  115.  
  116. allocate_storage()
  117. {
  118.   allocate_itemsets();
  119.  
  120.   shiftset = NEW2(nsyms, short);
  121.   redset = NEW2(nrules + 1, short);
  122.   state_table = NEW2(STATE_TABLE_SIZE, core *);
  123. }
  124.  
  125.  
  126.  
  127. free_storage()
  128. {
  129.   FREE(shift_symbol);
  130.   FREE(redset);
  131.   FREE(shiftset);
  132.   FREE(kernel_base);
  133.   FREE(kernel_end);
  134.   FREE(kernel_items);
  135.   FREE(state_table);
  136. }
  137.  
  138.  
  139.  
  140. /* compute the nondeterministic finite state machine (see state.h for details)
  141. from the grammar.  */
  142.  
  143. generate_states()
  144. {
  145.   allocate_storage();
  146.   initialize_closure(nitems);
  147.   initialize_states();
  148.  
  149.   while (this_state)
  150.     {
  151.       /* Set up ruleset and itemset for the transitions out of this state.
  152.          ruleset gets a 1 bit for each rule that could reduce now.
  153.      itemset gets a vector of all the items that could be accepted next.  */
  154.       closure(this_state->items, this_state->nitems);
  155.       /* record the reductions allowed out of this state */
  156.       save_reductions();
  157.       /* find the itemsets of the states that shifts can reach */
  158.       new_itemsets();
  159.       /* find or create the core structures for those states */
  160.       append_states();
  161.  
  162.       /* create the shifts structures for the shifts to those states,
  163.          now that the state numbers transitioning to are known */
  164.       if (nshifts > 0)
  165.         save_shifts();
  166.  
  167.       /* states are queued when they are created; process them all */
  168.       this_state = this_state->next;
  169.     }
  170.  
  171.   /* discard various storage */
  172.   finalize_closure();
  173.   free_storage();
  174.  
  175.   /* set up initial and final states as parser wants them */
  176.   augment_automaton();
  177. }
  178.  
  179.  
  180.  
  181. /* Find which symbols can be shifted in the current state,
  182.    and for each one record which items would be active after that shift.
  183.    Uses the contents of itemset.
  184.    shift_symbol is set to a vector of the symbols that can be shifted.
  185.    For each symbol in the grammer, kernel_base[symbol] points to
  186.    a vector of item numbers activated if that symbol is shifted,
  187.    and kernel_end[symbol] points after the end of that vector.  */
  188.  
  189. new_itemsets()
  190. {
  191.   register int i;
  192.   register int shiftcount;
  193.   register short *isp;
  194.   register short *ksp;
  195.   register int symbol;
  196.  
  197. #ifdef    TRACE
  198.   fprintf(stderr, "Entering new_itemsets\n");
  199. #endif
  200.  
  201.   for (i = 0; i < nsyms; i++)
  202.     kernel_end[i] = NULL;
  203.  
  204.   shiftcount = 0;
  205.  
  206.   isp = itemset;
  207.  
  208.   while (isp < itemsetend)
  209.     {
  210.       i = *isp++;
  211.       symbol = ritem[i];
  212.       if (symbol > 0)
  213.     {
  214.           ksp = kernel_end[symbol];
  215.  
  216.           if (!ksp)
  217.         {
  218.           shift_symbol[shiftcount++] = symbol;
  219.           ksp = kernel_base[symbol];
  220.         }
  221.  
  222.           *ksp++ = i + 1;
  223.           kernel_end[symbol] = ksp;
  224.     }
  225.     }
  226.  
  227.   nshifts = shiftcount;
  228. }
  229.  
  230.  
  231.  
  232. /* Use the information computed by new_itemset to find the state numbers
  233.    reached by each shift transition from the current state.
  234.  
  235.    shiftset is set up as a vector of state numbers of those states.  */
  236.  
  237. append_states()
  238. {
  239.   register int i;
  240.   register int j;
  241.   register int symbol;
  242.  
  243. #ifdef    TRACE
  244.   fprintf(stderr, "Entering append_states\n");
  245. #endif
  246.  
  247.   /* first sort shift_symbol into increasing order */
  248.  
  249.   for (i = 1; i < nshifts; i++)
  250.     {
  251.       symbol = shift_symbol[i];
  252.       j = i;
  253.       while (j > 0 && shift_symbol[j - 1] > symbol)
  254.     {
  255.       shift_symbol[j] = shift_symbol[j - 1];
  256.       j--;
  257.     }
  258.       shift_symbol[j] = symbol;
  259.     }
  260.  
  261.   for (i = 0; i < nshifts; i++)
  262.     {
  263.       symbol = shift_symbol[i];
  264.       shiftset[i] = get_state(symbol);
  265.     }
  266. }
  267.  
  268.  
  269.  
  270. /* find the state number for the state we would get to
  271. (from the current state) by shifting symbol.
  272. Create a new state if no equivalent one exists already.
  273. Used by append_states  */
  274.  
  275. int
  276. get_state(symbol)
  277. int symbol;
  278. {
  279.   register int key;
  280.   register short *isp1;
  281.   register short *isp2;
  282.   register short *iend;
  283.   register core *sp;
  284.   register int found;
  285.  
  286.   int n;
  287.  
  288. #ifdef    TRACE
  289.   fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  290. #endif
  291.  
  292.   isp1 = kernel_base[symbol];
  293.   iend = kernel_end[symbol];
  294.   n = iend - isp1;
  295.  
  296.   /* add up the target state's active item numbers to get a hash key */
  297.   key = 0;
  298.   while (isp1 < iend)
  299.     key += *isp1++;
  300.  
  301.   key = key % STATE_TABLE_SIZE;
  302.  
  303.   sp = state_table[key];
  304.  
  305.   if (sp)
  306.     {
  307.       found = 0;
  308.       while (!found)
  309.     {
  310.       if (sp->nitems == n)
  311.         {
  312.           found = 1;
  313.           isp1 = kernel_base[symbol];
  314.           isp2 = sp->items;
  315.  
  316.           while (found && isp1 < iend)
  317.         {
  318.           if (*isp1++ != *isp2++)
  319.             found = 0;
  320.         }
  321.         }
  322.  
  323.       if (!found)
  324.         {
  325.           if (sp->link)
  326.         {
  327.           sp = sp->link;
  328.         }
  329.           else   /* bucket exhausted and no match */
  330.         {
  331.           sp = sp->link = new_state(symbol);
  332.           found = 1;
  333.         }
  334.         }
  335.     }
  336.     }
  337.   else      /* bucket is empty */
  338.     {
  339.       state_table[key] = sp = new_state(symbol);
  340.     }
  341.  
  342.   return (sp->number);
  343. }
  344.  
  345.  
  346.  
  347. /* subroutine of get_state.  create a new state for those items, if necessary.  */
  348.  
  349. core *
  350. new_state(symbol)
  351. int symbol;
  352. {
  353.   register int n;
  354.   register core *p;
  355.   register short *isp1;
  356.   register short *isp2;
  357.   register short *iend;
  358.  
  359. #ifdef    TRACE
  360.   fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  361. #endif
  362.  
  363.   if (nstates >= MAXSHORT)
  364.     toomany("states");
  365.  
  366.   isp1 = kernel_base[symbol];
  367.   iend = kernel_end[symbol];
  368.   n = iend - isp1;
  369.  
  370.   p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  371.   p->accessing_symbol = symbol;
  372.   p->n